(function($){
	
	/**
	 * 配合queryForm进行分页查询
	 * */
	$.fn._pagination = function(options){
		var _default = {
			url : '',
			pn : 1,
			ps : 10,
			pc:null,
			total : 0,
			style:'',
			gotoPage:function(_page,_options){
				if(!_options.url){
					_options.url = $('.search-form').attr("action");
				}
				if(_options.url.lastIndexOf('?') == -1){
					_options.url += '?pageNumber=' + _page +"&pageSize=" + _options.ps;
				}else{
					options.url += '&pageNumber=' + _page +"&pageSize=" + _options.ps;
				}
				$('.search-form').attr("action", _options.url);
				$('.search-form').submit();
			}
		}
		var _options = $.extend(_default, options);
		if(!_options.pc){
			_options.pc =Math.ceil( _options.total / _options.ps );
		}
		function _initView($target){
			$($target).html('');
			var _vals = new Array();
			if(_options.pn > 4){
				_vals.push(1);
				_vals.push(0);
				_vals.push(_options.pn-2);
				_vals.push(_options.pn-1);
				_vals.push(_options.pn);
			}else{
				var i = 1;
				while(_options.pn >= i){
					_vals.push(i);
					i ++ ;
				}
			}
			var _ins = _vals.length <= 2 ? _vals.length + 2 : 2 ;
			if(_options.pn + _ins + 1 < _options.pc){
				for(var i = 1 ;i <= _ins; i++ ){
					_vals.push(_options.pn + i);
				}
				_vals.push(0);
				_vals.push(_options.pc);
			}else{
				for(var i = _options.pn ;i < _options.pc; i++ ){
					_vals.push(i+1);
				}
			}
			if(_vals.length == 0) return ;
			$target.append('<li class="prev" data-val='+ (_options.pn - 1) +'><a href="javascript:void(0);"><i class="ace-icon fa fa-angle-double-left"></i></a></li>');
			for(var i in _vals){
				$target.append('<li data-val="'+ _vals[i] +'"><a href="javascript:void(0);">'+ (_vals[i] == 0 ? '..' : _vals[i]) +'</a></li>');
			}
			$target.append('<li class="next" data-val='+ (_options.pn + 1) +'><a href="javascript:void(0);"><i class="ace-icon fa fa-angle-double-right"></i></a></li>');
			$target.find('li[data-val='+ _options.pn +']').addClass("active");
			$target.find('li[data-val=0]').addClass("disabled");
			if(_options.pn <= 1) $target.find("li.prev").addClass("disabled");
			if(_options.pn >= _options.pc) $target.find("li.next").addClass("disabled");
			$target.data(_options);
			//事件加载
			$target.find('li:not(".disabled,.active")').bind("click", function(){
				_options.gotoPage($(this).data("val"),_options);
			})
		}
		this.each(function(){
			var _target = $(this);
			if(_options.pc < 1) return ;
			_target.addClass("pagination pull-right no-margin");
			if(_options.style) _target.addClass("style");
			_initView(_target);
		})
		
	}
	
})(jQuery);